﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Interop;

namespace Win32Wrapper
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ListCollectionView sWindows;

        public MainWindow()
        {
            InitializeComponent();

            FillWithData();

            MessageBox.Show(Environment.Is64BitProcess.ToString());
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            source.AddHook(new HwndSourceHook(WndProc));
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == 0x1C || msg == 0x86)
            {
                FillWithData();
            }
            return IntPtr.Zero;
        }

        public void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            ListViewItem sItem = (ListViewItem)sender;
            Win32.Helpers.Window sWindow = (Win32.Helpers.Window)sItem.Content;

            Win32.Win32Helper.ShowWindow(sWindow.hWnd, Win32.ShowCommands.SW_SHOWNA);
            Win32.Win32Helper.SetForegroundWindow(sWindow.hWnd);
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            FillWithData();
        }

        private void FillWithData()
        {
            List<Win32.Helpers.Window> lWindows = Win32.Helpers.WindowHelper.GetDesktopWindows();
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Content.Process.ProcessName");

            List<ListViewItem> lList = new List<ListViewItem>();
            List<String> lProcess = new List<String>();

            foreach (Win32.Helpers.Window window in lWindows)
            {
                ListViewItem item = new ListViewItem();
                item.Content = window;
                item.MouseDoubleClick += ListViewItem_MouseDoubleClick;
                lList.Add(item);


                if (groupDescription.GroupNames.Contains(window.Process.ProcessName) == false)
                {
                    groupDescription.GroupNames.Add(window.Process.ProcessName);
                }
            }

            sWindows = new ListCollectionView(lList);

            List<object> SortedNames = groupDescription.GroupNames.OrderBy(s => s).ToList();

            groupDescription.GroupNames.Clear();
            foreach (var Name in SortedNames)
            {
                groupDescription.GroupNames.Add(Name);
            }

            sWindows.GroupDescriptions.Add(groupDescription);
            sWindows.SortDescriptions.Add(new System.ComponentModel.SortDescription("Content.Title", System.ComponentModel.ListSortDirection.Ascending));
            listView1.ItemsSource = sWindows;
            listView1.UpdateLayout();
            listView1.Focus();
        }

        private void ChangeTitleButton_Click(object sender, RoutedEventArgs e)
        {
            if (listView1.SelectedItem != null)
            {
                ListViewItem sItem = (ListViewItem)listView1.SelectedItem;
                Win32.Helpers.Window sWindow = (Win32.Helpers.Window)sItem.Content;

                Win32.Win32Helper.SetWindowText(sWindow.hWnd, ChangeTitleTextBox.Text);
            }
        }
    }
}
